package com.zhb.cloud.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

/**
 * @Title: RedisConfig.java
 * @Package com.zzh.sys.configuration
 * @Description: redis对httpsession的支持,做到session在多个服务器间共享
 * @author zhb
 * @date 2016年3月6日 下午8:03:19
 * @version V1.0
 */
@Configuration
@EnableRedisHttpSession
public class RedisConfig {
	/**
	 * @Title: redisTemplate
	 * @Description: 操作模板
	 * @return: RedisTemplate<Object,Object>
	 * @param: @return
	 * @author: zhb
	 * @throws 2016年3月6日下午8:11:33
	 */
	@Autowired
	JedisConnectionFactory jedisConnectionFactory;

	public @Bean RedisTemplate<Object, Object> redisTemplate() {
		RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(jedisConnectionFactory);
		return redisTemplate;
	}

	public @Bean ShardedJedisPool shardedJedisPool() {
		JedisPoolConfig poolConfig = jedisConnectionFactory.getPoolConfig();
		JedisShardInfo shardInfo = jedisConnectionFactory.getShardInfo();
		List<JedisShardInfo> shardInfos = new ArrayList<>();
		shardInfos.add(shardInfo);
		return new ShardedJedisPool(poolConfig, shardInfos);
	}

	/**
	 * @Title: getMessageListenerAdapter
	 * @Description: 消息监听器
	 * @return: MessageListenerAdapter
	 * @author: zhb
	 * @throws 2016年4月14日上午9:40:10
	 */
	public @Bean(name = "messageListener") MessageListenerAdapter getMessageListenerAdapter() {
		return new MessageListenerAdapter();
	}

	/**
	 * @Title: getTopic
	 * @Description: 聊天室
	 * @return: Topic
	 * @param: @return
	 * @author: zhb
	 * @throws 2016年4月14日上午9:54:45
	 */
	public @Bean(name = "topic") Topic getTopic() {
		return new ChannelTopic("chartroom");
	}

	/**
	 * @Title: getRedisMessageListenerContainer
	 * @Description: 获得redis消息监听容器
	 * @return: RedisMessageListenerContainer
	 * @param: @return
	 * @author: zhb
	 * @throws 2016年4月14日上午9:41:09
	 */
	public @Bean(name = "redisContainer") RedisMessageListenerContainer getRedisMessageListenerContainer() {
		Map<MessageListenerAdapter, Collection<? extends Topic>> listeners = new HashMap<>();
		List<Topic> topics = new ArrayList<>();
		topics.add(getTopic());
		listeners.put(getMessageListenerAdapter(), topics);
		RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
		listenerContainer.setConnectionFactory(jedisConnectionFactory);
		listenerContainer.setMessageListeners(listeners);
		return listenerContainer;
	}
}
